package com.chris.easymallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chris.common.utils.PageUtils;
import com.chris.common.utils.Query;

import com.chris.easymallproduct.dao.SkuInfoDao;
import com.chris.easymallproduct.entity.SkuInfoEntity;
import com.chris.easymallproduct.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> pageWrapper = new Query<SkuInfoEntity>().getPageWrapper(params, SkuInfoEntity.class);

//        catelogId: 225
//        brandId: 3
//        min: 3
//        max: 500
        LambdaQueryWrapper<SkuInfoEntity> lambda = pageWrapper.lambda();
        Object catelogId = params.get("catelogId");
        if (catelogId != null && catelogId.toString().matches("[1-9][0-9]*$")) {
            lambda.eq(SkuInfoEntity::getCatalogId, catelogId);
        }
        Object brandId = params.get("brandId");
        if (brandId != null && brandId.toString().matches("[1-9][0-9]*$")) {
            lambda.eq(SkuInfoEntity::getBrandId, brandId);
        }
        String min = (String) params.get("min");
        String max = (String) params.get("max");
        if (min != null && min.matches("^[0-9]+$")) {
            lambda.ge(SkuInfoEntity::getPrice, new BigDecimal(min));
        }
        if (max != null && max.matches("[1-9][0-9]*$")) {
            lambda.le(SkuInfoEntity::getPrice, new BigDecimal(max));
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                pageWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void saveInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

}